/**
 * 第三页作业审批 - 延期作业审批，有查看和修改两个操作，修改仅仅添加isPass和reason两个字段。
 * 查看需要根据id返回 作业票、延期申请、审批意见三张表，修改需要操作“延期审批记录表”
 */


package com.xidian.zhiancloud.controller.dangerWorkTicket.workApprove;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.DelayApplicationApprove;
import com.xidian.zhiancloud.qo.dangerWorkTicket.WorkTicketQO;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkApprove.DelayApplicationApproveService;
import com.xidian.zhiancloud.service.dangerWorkTicket.WorkTicket.WorkTicketService;
import com.xidian.zhiancloud.vo.dangerWorkTicket.WorkTicketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/DelayApplicationApprove")
public class DelayApplicationApproveController extends BaseController {
    @Autowired
    private DelayApplicationApproveService delayApplicationApproveService;

    @Autowired
    private WorkTicketService workTicketService;


    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 根据条件查询作业票
     *
     * @param workTicketQO
     * @return
     */
    @GetMapping("/getListByQO")
    public ResultDTO<IPage<WorkTicketVO>> getListByQO(WorkTicketQO workTicketQO) {
        if (workTicketQO.getCurrent() == null || workTicketQO.getSize() == null) {
            return ResultDTO.errorOf(GeneralResponseEnums.REQUEST_ERROR);
        }
        workTicketQO.setCompanyId(getSessionUserCompanyId());
        return ResultDTO.successOf(delayApplicationApproveService.getListByQO(workTicketQO), GeneralResponseEnums.SUCCESS);
    }

    @PostMapping("/approveDelayWork")
    public ResultDTO<Boolean> approveDelayWork( @RequestParam("id") Long id,
                                                @RequestParam("approveResult") Integer approveResult,
                                                @RequestParam("approveView") String approveView) {

        delayApplicationApproveService.getById(id);

        DelayApplicationApprove delayApplicationApprove = new DelayApplicationApprove();
        delayApplicationApprove.setCompanyId(getSessionUserCompanyId());
        delayApplicationApprove.setApproverId(getSessionUserid());
        delayApplicationApprove.setRelateWorkId(id);
        delayApplicationApprove.setApproveTime(new Date(System.currentTimeMillis()));
        delayApplicationApprove.setApproveResult(approveResult);
        delayApplicationApprove.setApproveView(approveView);
        delayApplicationApprove.setApproveOrganizationId(Long.parseLong(getSessionUser().getOrgids().split(",")[0]));

        return ResultDTO.successOf(delayApplicationApproveService.save(delayApplicationApprove), GeneralResponseEnums.SUCCESS);
    }

}
